<template>
<div>
   <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="实验名称">
        <el-select v-model="dataForm.uuid" placeholder="请选择"style="width: 200px;"clearable>
            <el-option
              v-for="item in experimentName"
              :key="item.uuid"
              :label="item.experimentName"
              :value="item.uuid">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="实验进度">
        <el-select v-model="dataForm.experimentFlag" placeholder="请选择" style="width: 140px;" clearable>
            <el-option
              v-for="item in experimentFlag"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="实验报告状态">
        <el-select v-model="dataForm.workFlag" placeholder="请选择"style="width: 140px;"clearable>
            <el-option
              v-for="item in workFlag"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="学生名称">
        <el-input v-model="dataForm.userName" placeholder="张宏" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success"  plain size="mini" @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      :default-sort = "{order: 'descending'}"
      style="width: 100;">
      <el-table-column
        prop="userId"
        type="index"
        fixed="left"
        header-align="center"
        align="center"
        width="80"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="experimentName"
        header-align="center"
        width="200"
        fixed="left"
        align="center"
        label="实验名称">
      </el-table-column>
      <el-table-column
        prop="studentAccount"
        header-align="center"
        align="center"
        label="学生账号">
      </el-table-column>
      <el-table-column
        prop="studentName"
        header-align="center"
        align="center"
        label="学生姓名">
      </el-table-column>
      <el-table-column
        prop="studentBelong"
        header-align="center"
        align="center"
        width="180px"
        label="学生所属">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="实验状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">已关闭</el-tag>
          <el-tag v-else size="small" type="success">开启中</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="experimentFlag"
        header-align="center"
        align="center"
        label="实验进度">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.experimentFlag === 0" size="small" type="primary">未开始</el-tag>
          <el-tag v-if="scope.row.experimentFlag === 1" size="small" type="success">进行中</el-tag>
          <el-tag v-if="scope.row.experimentFlag === 2" size="small" type="danger">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="workFlag"
        header-align="center"
        width="120px"
        align="center"
        label="实验报告状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.workFlag === 0" size="small" type="primary">未完成</el-tag>
          <el-tag v-if="scope.row.workFlag === 1" size="small" type="danger">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="score"
        header-align="center"
        align="center"
        sortable
        label="成绩">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="350"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="primary"  @click="viewHandle(scope.row.experimentUuid)"  plain size="mini">详情</el-button>
          <el-button  @click="ChangeScoreHandle(scope.row.id,scope.row.studentName,scope.row.studentBelong,scope.row.studentAccount,scope.row.score)"
          :type="scope.row.workFlag == 0? 'info' : 'success'" :disabled="scope.row.workFlag == 0? true : false"  plain size="mini">{{scope.row.workFlag == 0 ? '不可批阅' : '点击批阅'}}</el-button>
          <el-button  type="danger"  plain size="mini" @click="pullHandle(scope.row.experimentFile)"
          :disabled="scope.row.workFlag == 0? true : false">实验报告下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[9, 18, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <teacher-experiment-view v-if="addOrUpdateVisible" ref="viewHandle"></teacher-experiment-view>
    <change-score v-if="addOrUpdateVisible" ref="changeHandle" @refreshDataList="getDataList"></change-score>
  </div>
</template>
<script>
  import TeacherExperimentView from './teacher-experiment-view.vue'
  import ChangeScore from './change-score.vue'
export default {
  /* eslint-disable */
  components: {TeacherExperimentView,ChangeScore},
    data () {
      return {
        experimentFlag:[{value: '0',label: '未开始'}, {value: '1',label: '进行中'},{value: '2',label: '已完成'}],
        workFlag: [{value: '0',label: '未完成'}, {value: '1', label: '已完成'}],
        experimentName: [],
        uuids:[],
        disabled: true,
        isShow: false,
        dataForm: {
          studentName: '',
          uuid: '',
          status: '',
          experimentFlag: '',
          workFlag: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 9,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        uploadImageVisible: false,
      }
    },
    computed: {
      id: {
        get () { return this.$store.state.user.id },
        set (val) { this.$store.commit('user/updateId', val) }
      },
      account: {
        get () { return this.$store.state.user.account },
        set (val) { this.$store.commit('user/updateAccount', val) }
      },
      avatar: {
        get () { return this.$store.state.user.avatar },
        set (val) { this.$store.commit('user/updateAvatar', val) }
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      //修改分数
      ChangeScoreHandle (id,studentName,studentBelong,studentAccount,score) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.changeHandle.init(id,studentName,studentBelong,studentAccount,score)
        })
      },
      // 获取所有的实验
      getExperimentList () {
        this.$http({
          url: this.$http.adornUrl('/course/getExperimentName'),
            method: 'post',
            data: this.$http.adornData({
              teacherAccount: this.account
            })
        }).then(({data}) => {
          if(data.success){
            this.experimentName = data.data
          } else {
            this.$message.error(data.message)
          }
        })
      },
      // 获取数据列表
      getDataList () {
        this.getExperimentList ()
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/courses/getBatchStudentExperiment'),
          method: 'post',
          data: this.$http.adornData({
            'teacherAccount':this.account,
            'experimentUuid': this.dataForm.uuid,
            'studentName': this.dataForm.studentName,
            'enableFlag': 1,
            'experimentFlag':this.dataForm.experimentFlag,
            'workFlag': this.dataForm.workFlag,
            'pageNo': this.pageIndex,
            'pageSize': this.pageSize
          })
        }).then(({data}) => {
          if (data.success) {
            this.dataList = data.data.rows
            this.totalPage = data.data.rowTotal
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.message)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      viewHandle (experimentUuid){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.viewHandle.init(experimentUuid)
        })
      },
      pullHandle (experimentFile){
        window.location.href = experimentFile
      },
      selectHandle (uuid){
        this.$confirm('确定要选择该课程?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/courses/selectedExperiment'),
            method: 'post',
            data: this.$http.adornData({
              'uuid': uuid,
              'account': this.$store.state.user.account
            })
          }).then(({data}) => {
            if (data.success) {
              this.$message({
                message: '你已经成功选中了该课程！',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.message)
            }
          })
        })
      }
    }
  }
</script>
